package com.hz.control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import com.hz.dbconn.DBConnect;
import com.hz.model.Activity;
import com.hz.model.User;
import com.hz.util.Pagination;

public class ActivityControl {
    public static Pagination pageInstance = null;
	
       /*
        * 新增活动
        * 参数（Activity）
        * 返回值：boolean
        */
	public static boolean insertActivity(Activity activity){
		DBConnect dbc = new DBConnect();
		String sql = "insert into activity(actName,actType,peopleNum,startTime,endTime,place," +
				"cost,costDetail,resource,leader,content,departmentId)values(?,?,?,?,?,?,?,?,?,?,?,?)";
		dbc.prepareStatement(sql);
		try{
		       int index = 1;
		       dbc.setString(index++, activity.getActName());
		       dbc.setInt(index++, activity.getActType());
		       dbc.setInt(index++,activity.getPeopleNum());
		       dbc.setString(index++,activity.getStartTime());
		       dbc.setString(index++,activity.getEndTime());
		       dbc.setString(index++,activity.getPlace());
		       dbc.setInt(index++,activity.getCost());
		       dbc.setString(index++,activity.getCostDetail());
		       dbc.setString(index++,activity.getResource());
		       dbc.setString(index++,activity.getLeader());
		       dbc.setString(index++,activity.getContent());
		       dbc.setInt(index++,activity.getDepartmentId());
			   dbc.executeUpdate();
			   return true;
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("查询数据库，插入新活动失败!");
			return false;
		}finally{
			dbc.close();
		}
	}



 
              /*根据id获取活动信息
	        * 参数（id）
	        * 返回值：Activity
	        */
        public static  Activity getActivityInformation(int id){
	      DBConnect dbc = new DBConnect();
	      String sql = "SELECT * FROM activity WHERE id='"+id+"'";
	      dbc.prepareStatement(sql);
	      try{
		    Activity activity=new Activity();
	            ResultSet rs = dbc.executeQuery();
	           if(rs.next())
	            {
	                 activity.setId(id);
		         activity.setActName(rs.getString("actName"));
		         activity.setActType(rs.getInt("actType"));
		         activity.setPeopleNum(rs.getInt("peopleNum"));
	   	         activity.setStartTime(rs.getString("startTime"));
	 	         activity.setEndTime(rs.getString("endTime"));
	 	         activity.setPlace(rs.getString("place"));
	 	         activity.setCost(rs.getInt("cost"));
	 	         activity.setCostDetail(rs.getString("costDetail"));
	 	         activity.setResource(rs.getString("resource"));
	 	         activity.setLeader(rs.getString("leader"));
	 	         activity.setContent(rs.getString("content"));
	 	         activity.setDepartmentId(rs.getInt("departmentId"));
	 	         return activity;
	             }
            } catch (SQLException e) {
	             e.printStackTrace();
	             System.out.println("获取活动信息失败!");
	             return null;
              } finally {
	                    dbc.close();
             }
            return null;
   }
   
       /*删除活动
        * 参数（id）
        * 返回值：boolean
        */
        public  static boolean deleteActivity(int id){
	     DBConnect dbc = new DBConnect();
	     String sql = "DELETE FROM activity WHERE id=?";
	     dbc.prepareStatement(sql);
	     try {
		   dbc.setInt(1, id);
		   dbc.executeUpdate();
		   return true;
	      } catch (SQLException e) {
		   e.printStackTrace();
		   return false;
	     } finally {
		dbc.close();
	  }
   }
  
           /*修改活动内容
            * 参数（Activity,id）
            * 返回值：boolean
            */
           public static  boolean updateActivity(Activity activity,int id){
        	DBConnect dbc = new DBConnect();
	        String sql = "UPDATE activity SET actName=?,actType=?,peopleNum=?,startTime=?,endTime=?," +
	        	      "place=?,cost=?,costDetail=?,resource=?,leader=?,content=?,departmentId=? WHERE id=?";
	        dbc.prepareStatement(sql);
	        try {  
	               int index = 1;
		       dbc.setString(index++, activity.getActName());
		       dbc.setInt(index++, activity.getActType());
		       dbc.setInt(index++,activity.getPeopleNum());
		       dbc.setString(index++,activity.getStartTime());
		       dbc.setString(index++,activity.getEndTime());
		       dbc.setString(index++,activity.getPlace());
		       dbc.setInt(index++,activity.getCost());
		       dbc.setString(index++,activity.getCostDetail());
		       dbc.setString(index++,activity.getResource());
		       dbc.setString(index++,activity.getLeader());
		       dbc.setString(index++,activity.getContent());
		       dbc.setInt(index++,activity.getDepartmentId());
		       dbc.setInt(index++, id);
		       dbc.executeUpdate();
		       return true;
	           } catch (SQLException e) {
		        e.printStackTrace();
		        System.out.println("更新活动信息失败");
		       return false;
	          } finally {
		       dbc.close();
	    }
   }
          
           //遍历所有活动以供选择
           public static Collection<Activity> getActivities() {
               DBConnect dbc = new DBConnect();
               String sql = "select * from activity order by endtime desc";
               Collection<Activity> actList = new ArrayList<Activity>();
               ResultSet rs = null;
               dbc.prepareStatement(sql);
		try {  
		       rs = dbc.executeQuery();
			while (rs.next()) {
			         Activity activity = new Activity();
			         activity.setId(rs.getInt("id"));
			         activity.setActName(rs.getString("actName"));
			         activity.setActType(rs.getInt("actType"));
			         activity.setPeopleNum(rs.getInt("peopleNum"));
		   	         activity.setStartTime(rs.getString("startTime"));
		 	         activity.setEndTime(rs.getString("endTime"));
		 	         activity.setPlace(rs.getString("place"));
		 	         activity.setCost(rs.getInt("cost"));
		 	         activity.setCostDetail(rs.getString("costDetail"));
		 	         activity.setResource(rs.getString("resource"));
		 	         activity.setLeader(rs.getString("leader"));
		 	         activity.setContent(rs.getString("content"));
		 	         activity.setDepartmentId(rs.getInt("departmentId"));
				 actList.add(activity);
				 activity = null;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
		    dbc.close();
		}
		return actList;
	    
	}
           
   
   public static Collection getActivities(int page, int rowsPerPage) {
	        String sql = "select * from activity order by id desc";
	        //System.out.println(sql);
	        pageInstance = new ActivityControlPage(sql, rowsPerPage);
		try {
		     return pageInstance.getPage(page);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

class ActivityControlPage extends Pagination {
	
	public ActivityControlPage(String sql, int rowsPerPage) {
		super(sql, rowsPerPage, 1);
	}

	public Collection<Activity> packResultSet(ResultSet rs) {
		Collection<Activity> actList = new ArrayList<Activity>();
		try {
			while (rs.next()) {
			         Activity activity = new Activity();
			         activity.setId(rs.getInt("id"));
			         activity.setActName(rs.getString("actName"));
			         activity.setActType(rs.getInt("actType"));
			         activity.setPeopleNum(rs.getInt("peopleNum"));
		   	         activity.setStartTime(rs.getString("startTime"));
		 	         activity.setEndTime(rs.getString("endTime"));
		 	         activity.setPlace(rs.getString("place"));
		 	         activity.setCost(rs.getInt("cost"));
		 	         activity.setCostDetail(rs.getString("costDetail"));
		 	         activity.setResource(rs.getString("resource"));
		 	         activity.setLeader(rs.getString("leader"));
		 	         activity.setContent(rs.getString("content"));
		 	         activity.setDepartmentId(rs.getInt("departmentId"));
				 actList.add(activity);
				 activity = null;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return actList;
	}
   
   
}

